/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.folder.stdPatterns.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import net.yienyien.process.bpd.BPDComponent;
import net.yienyien.process.bpd.BPDObject;
import net.yienyien.process.bpd.BpdPackage;
import net.yienyien.process.bpd.ControlFlowRelation;
import net.yienyien.process.folder.stdPatterns.Other;
import net.yienyien.process.folder.stdPatterns.StdPatternsPackage;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Other</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class OtherImpl extends StdBPDPatternImpl implements Other {
	
	public static final String NAME="Other";
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	protected OtherImpl() {
		super(NAME, BpdPackage.eINSTANCE.getBPDObject(), BpdPackage.eINSTANCE.getBPDObject(), BpdPackage.eINSTANCE.getTask(), BpdPackage.eINSTANCE.getEvent(), BpdPackage.eINSTANCE.getParallelFork(), BpdPackage.eINSTANCE.getParallelJoin(), BpdPackage.eINSTANCE.getDataBasedXOR(), BpdPackage.eINSTANCE.getXORMerge());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return StdPatternsPackage.Literals.OTHER;
	}

	@Override
	public EList<BPDObject> getContent(BPDObject source, BPDObject sink) {
		HashSet<BPDObject> set=new HashSet<BPDObject>();
		
		if(grabPath(source, sink, true, set)) {
			return null;
		}
		
		if(grabPath(source, sink, false, set)) {
			return null;
		}
	
		EList<BPDObject> setp=new BasicEList<BPDObject>();
		setp.addAll(set);
		BPDComponent comp=find(setp); // Recursive call for minimal search
		if(comp!=null) {
			setp.clear();
			setp.remove(comp.getSource());
			setp.remove(comp.getSink());
		}
		
		return setp;
	}

	@Override
	public boolean verify(BPDComponent component) {
		return true;
	}
	
	private void grabPath(BPDObject current, BPDObject goal, boolean forward, ArrayList<BPDObject> stack, Collection<BPDObject> objects) {
		if(current.equals(goal)) {
			objects.addAll(stack);
			return;
		}
		
		if(stack.contains(current)) {
			return;
		}
		
		stack.add(current);
		for(ControlFlowRelation r: (forward)?current.getOutputs(): current.getInputs()) {
			grabPath(((forward)?r.getOutput():r.getInput()), goal, forward, stack, objects);
		}
		
		return;
	}
	
	private boolean grabPath(BPDObject source, BPDObject sink, boolean forward, Collection<BPDObject> set) {
		ArrayList<BPDObject> stack=new ArrayList<BPDObject>();
		
		
		if(forward) {
			grabPath(source, sink, true, stack, set);
		} else {
			grabPath(sink, source, false, stack, set);
		}
		
		return true;
	}

} //OtherImpl
